// pages/blue/index.js
const app = getApp();
const crcUtils = require("../../utils/crc.js");
const utils = require("../../utils/util.js");
const console_log = require("../../utils/logUtils");
var base64 = require("../../utils/base64.modified.js"); //引用
const baseUrl = app.globalData.mofitUrl;
const htmlSnip =
  `<div>
<dl>
<dt style='
font-size: 15px;
font-weight: normal;
font-stretch: normal;
line-height: 25px;
letter-spacing: 1px;
color: white;text-align:left'>昊庆微智能科技</dt>
<dt style='color: white;font-size: 16px;font-weight: bold;letter-spacing: 1px;'>更高效、更智能，更快捷的<a style='
font-size: 15px;font-weight:normal;letter-spacing: 1px;'>解决您的烦恼</a></dt>
</dl>
</div>
`
const logo_tip = `<p style='color:white;font-size:30px'><a style='color: white;font-weight: bold;letter-spacing: 1rpx;'>开启,</a> 智能时代！</p>`


Page({
  data: {
    total: '',
    isClick: false,
    isTrainClick: false,
    isMeClick: false,
    htmlSnip,
    logo_tip,
  },
  onLoad: function () {
    this.keepMainScreenOn();
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');

    that.setData({
      htmlSnip: htmlSnip,
      logo_tip: logo_tip
    })
    wx.getSystemInfo({
      success: (result) => {
        app.globalData.sysinfo = result
      },
    })
    console.log("system info:",app.globalData.sysinfo)
  },
  onShow() {
    let t='7B01010700FFFF06010118C700004294FF'
    let crcStr= crcUtils.ToCRC16(t, false);
    let a=[11,0]
    
    console.log("crc str:",utils.bytesToLong(a))
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.closeBluetoothAdapter({
      success: function (res) {
        console_log.log("mainview on unload keep on");
      }
    });
  },
  touchStartEms: function () {
    var that = this;
    that.setData({
      isClick: true
    });
  },
  touchEndEms: function () {
    var that = this;
    that.setData({
      isClick: false
    });
    wx.navigateTo({
      // url: "/pages/device-management/index?isAutoConnect=1",
      url:"/pages/device-management/index"
    });
  },
  startEms: function () {
    var that = this;
    this.setData({
      isClick: !this.data.isClick
    });
  },
  gotoEms: function () {
    wx.navigateTo({
      url: "/pages/device-management/index"
    });
  },
  //重新登录
  showRelogin: function (contentStr) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: contentStr,
      showCancel: false,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: app.globalData.buttonStyleColor,
      success: (result) => {
        if (result.confirm) {
          that.jumpToRegister();
        }
      },
      fail: () => {},
      complete: () => {}
    });
  },
  // 解析crc
  parseCrc: function (e) {
    if (!e) return null;
    let crc = crcUtils.ToCRC16(e, false);
    console.log("crc 计算+" + crc);
    return crc;
  },
  // 屏幕常量
  keepMainScreenOn: function () {
    wx.setKeepScreenOn({
      keepScreenOn: true,
      success: result => {
        console_log.log("screen keep on");
      },
      fail: () => {},
      complete: () => {}
    });
  },
  //显示成功绑定
  showBindSuccess: function (userInfo) {
    var that = this;
    wx.showToast({
      title: "登录成功!",
      icon: "none",
      image: "",
      duration: 2500,
      mask: false,
      success: result => {},
      fail: () => {},
      complete: () => {
        that.jumpToMainView(userInfo);
      }
    });
  },
  //jump to main view
  jumpToMainView: function (userInfo) {
    var that = this;
    // let user = JSON.stringify(userInfo);
    wx.setStorage({
      key: "userInfo",
      data: userInfo,
      success: result => {
        utils.sleepTime(1500);
        that.getCurrentWeekTrainCulor();
      },
      fail: () => {},
      complete: () => {}
    });
  },
  //清除缓存的训练记录数据
  clearTrainHistory: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  touchStartMe: function () {
    var that = this;
    that.setData({
      isMeClick: true
    });
  },
  touchEndMe: function () {
    var that = this;
    that.setData({
      isMeClick: false
    });
  },
  touchStartHistory: function () {
    var that = this;
    that.setData({
      isTrainClick: true
    });
  },
  touchEndHistory: function () {
    var that = this;
    that.setData({
      isTrainClick: false
    });
  },
  
});